export function dateFormat (fmt, date) {
  if (!date) { return '' }

  date = new Date(date)
  let ret
  const opt = {
    'Y+': date.getFullYear().toString(), // 年
    'm+': (date.getMonth() + 1).toString(), // 月
    'd+': date.getDate().toString(), // 日
    'H+': date.getHours().toString(), // 时
    'M+': date.getMinutes().toString(), // 分
    'S+': date.getSeconds().toString() // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  }
  for (const k in opt) {
    ret = new RegExp(`(${k})`).exec(fmt)
    // eslint-disable-next-line eqeqeq
    if (ret) { fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0'))) }
  }
  ;
  return fmt
}

export function weekDay (date) {
  const d = date.replace('年', '-').replace('月', '-').replace('日', '-')
  const dt = new Date(d.split('-')[0], d.split('-')[1] - 1, date = d.split('-')[2])
  const weekDay = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  return weekDay[dt.getDay()]
}
export function fmtWeekDay (date) {
  const d = new Date(date)
  const weekDay = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  return weekDay[d.getDay()]
}

export function parseDate (date) {
  const t = Date.parse(date)
  if (!isNaN(t)) { return new Date(Date.parse(date.replace(/-/g, '/'))) } else { return new Date() }
}
export function dateFmt (date) {
  date += ''
  // yyyyMMddHHmmss
  const year = date.substring(0, 4)
  const month = date.substring(4, 6)
  const day = date.substring(6, 8)
  const hours = date.substring(8, 10)
  const minutes = date.substring(10, 12)
  const seconds = date.substring(12, 14)
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
export function timestampToTime (timestamp) {
  const time = new Date(timestamp)
  const year = time.getFullYear()
  let month = time.getMonth() + 1
  let date = time.getDate()
  let hours = time.getHours()
  let minute = time.getMinutes()
  let second = time.getSeconds()

  if (month < 10) { month = `0${month}` }

  if (date < 10) { date = `0${date}` }

  if (hours < 10) { hours = `0${hours}` }

  if (minute < 10) { minute = `0${minute}` }

  if (second < 10) { second = `0${second}` }

  return `${year}-${month}-${date} ${hours}:${minute}:${second}`
}
// 获取days之前的日期
export function getBeforeDateNoFormat (date, days) {
  const base = new Date(date).getTime()

  const oneDay = 24 * 3600 * 1000

  const now = new Date(base - oneDay * (days - 1))

  return now
}

// 获取days之前的日期
export function getBeforeDate (date, days) {
  const base = new Date(date).getTime()

  const oneDay = 24 * 3600 * 1000

  const now = new Date(base - oneDay * (days - 1))

  return dateFormat('YYYY-mm-dd', now)
}

export function getBeforeDates (date, days) {
  const base = new Date(date).getTime()

  const oneDay = 24 * 3600 * 1000

  const dates = []
  for (let i = days - 1; i >= 0; i--) { dates.push(dateFormat('YYYY-mm-dd', new Date(base - oneDay * i))) }

  dates.push(dateFormat('YYYY-mm-dd', date))
  return dates
}

export function dateDiffDates (fromDate, toDate) {
  const from = new Date(fromDate).getTime()
  const oneDay = 24 * 3600 * 1000

  const dates = []
  let i = 0
  while (dateFormat('YYYY-mm-dd', new Date(from + oneDay * i)) !== toDate) {
    dates.push(dateFormat('YYYY-mm-dd', new Date(from + oneDay * i)))
    i++
  }
  dates.push(dateFormat('YYYY-mm-dd', new Date(toDate)))
  return dates
}

export function dayToDate (date, begin) {
  const from = date.getTime()
  const oneDay = 24 * 3600 * 1000
  if (begin) { return `${dateFormat('YYYY-mm-dd', date)} 00:00:00` } else { return `${dateFormat('YYYY-mm-dd', new Date(from + oneDay))} 00:00:00` }

  // eslint-disable-next-line no-unreachable, no-undef
  return dates
}

export function checkDate (value) {
  const reg = '^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|          (?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$'
  const regExp = new RegExp(reg)
  if (!regExp.test(value)) { return false } else { return true }
}
/**
 * 获取跳过多少时间的一天开始
 * @param skip
 */
export function getStartDate (skip) {
  const time = 24 * 60 * 60 * 1000 * (skip || 0)
  const date = new Date()
  date.setTime(date.getTime() + time)
  date.setHours(0)
  date.setMinutes(0)
  date.setSeconds(0)
  return date
}

export function getEndDate (skip) {
  const time = 24 * 60 * 60 * 1000 * (skip || 0)
  const date = new Date()
  date.setTime(date.getTime() + time)
  date.setHours(23)
  date.setMinutes(59)
  date.setSeconds(59)
  return date
}
